package com.javasm.finance.controller;

import com.javasm.finance.entity.*;
import com.javasm.finance.service.ProductCategoryService;
import com.javasm.finance.service.impl.ProductCategoryServiceImpl;
import com.javasm.finance.util.RespUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/series/*")
public class Prod1Controller extends BaseController{
    ProductCategoryService productCategoryService=new ProductCategoryServiceImpl();

    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //当前页
        String pageStr = req.getParameter("page");
        Integer page = 1;
        if (pageStr != null && !"".equals(pageStr)) {
            page = Integer.valueOf(pageStr);
        }
        //每页显示的数据条数
        String pageSizeStr = req.getParameter("pageSize");
        Integer pageSize = 5;
        if (pageSizeStr != null && !"".equals(pageSizeStr)) {
            pageSize = Integer.valueOf(pageSizeStr);
        }
        String productCategoryName = req.getParameter("prodChineseName");
        //调用方法
        List<ProductCategory> productCategories = productCategoryService.queryProductCategory(page, pageSize, productCategoryName);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (productCategories != null && productCategories.size() > 0) {
            //查询数据总条数
            long totalNum = productCategoryService.findTotalNum(productCategoryName);
            PageInfo pageInfo = new PageInfo(page, pageSize, totalNum);
            //创建Map集合
            Map<String, Object> returnMap = new HashMap<>();
            returnMap.put("productCategories", productCategories);
            returnMap.put("pageInfo", pageInfo);
            re.setReturnCode(CodeAndMsg.QUERY_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_SUCCESS.getReturnMsg());
            re.setReturnData(returnMap);
        } else {
            //创建Map集合
            Map<String, Object> returnMap = new HashMap<>();
            returnMap.put("pageInfo", new PageInfo(1, 5, 0l));
            re.setReturnCode(CodeAndMsg.QUERY_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_FAILED.getReturnMsg());
            re.setReturnData(returnMap);
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String prodParentChannel = req.getParameter("prodParentChannel");
        String prodFirsClass = req.getParameter("prodFirsClass");
        String prodChineseName = req.getParameter("prodChineseName");
        String prodEnglishName = req.getParameter("prodEnglishName");
        String prodChannelName = req.getParameter("prodChannelName");
        ProductCategory productCategory=new ProductCategory(prodChineseName,prodEnglishName,prodParentChannel,prodFirsClass,prodChannelName);

        int rows = productCategoryService.addProductCategory(productCategory);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (rows > 0) {
            re.setReturnCode(CodeAndMsg.OPERATE_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_SUCCESS.getReturnMsg());
        } else {
            re.setReturnCode(CodeAndMsg.OPERATE_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        String prodIdStr = req.getParameter("prodId");
        Integer prodId = null;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.valueOf(prodIdStr);
        }

        ProductCategory byPid = productCategoryService.findByPid(prodId);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (byPid != null) {
            re.setReturnCode(CodeAndMsg.QUERY_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_SUCCESS.getReturnMsg());
            re.setReturnData(byPid);
        } else {
            re.setReturnCode(CodeAndMsg.QUERY_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String prodIdStr = req.getParameter("prodId");
        Integer prodId = null;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.valueOf(prodIdStr);
        }
        String prodParentChannel = req.getParameter("prodParentChannel");
        String prodFirsClass = req.getParameter("prodFirsClass");
        String prodChineseName = req.getParameter("prodChineseName");
        String prodEnglishName = req.getParameter("prodEnglishName");
        ProductCategory productCategory=new ProductCategory(prodId,prodChineseName,prodEnglishName,prodParentChannel,prodFirsClass);
        int rows = productCategoryService.editProd(productCategory);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (rows > 0) {
            re.setReturnCode(CodeAndMsg.OPERATE_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_SUCCESS.getReturnMsg());
        } else {
            re.setReturnCode(CodeAndMsg.OPERATE_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void queryChannel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        List<ProdChannel> list = productCategoryService.queryChannel();
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (list != null && list.size() > 0) {
            re.setReturnCode(CodeAndMsg.QUERY_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_SUCCESS.getReturnMsg());
            re.setReturnData(list);
        } else {
            re.setReturnCode(CodeAndMsg.QUERY_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void toRemi(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        String prodIdStr = req.getParameter("prodId");
        Integer prodId = null;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.valueOf(prodIdStr);
        }

        ProductCategory byPid = productCategoryService.findByPidRemi(prodId);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (byPid != null) {
            re.setReturnCode(CodeAndMsg.QUERY_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_SUCCESS.getReturnMsg());
            re.setReturnData(byPid);
        } else {
            re.setReturnCode(CodeAndMsg.QUERY_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void remi(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String prodIdStr = req.getParameter("prodId");
        Integer prodId = null;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.valueOf(prodIdStr);
        }
        String prodChannelName = req.getParameter("prodChannelName");
        String remiBankName = req.getParameter("remiBankName");
        String remiSwift = req.getParameter("remiSwift");
        String remiBankCode = req.getParameter("remiBankCode");
        String remiCnaps = req.getParameter("remiCnaps");
        String remiBankAddress = req.getParameter("remiBankAddress");
        String remiPeopleName = req.getParameter("remiPeopleName");
        String remiAccount = req.getParameter("remiAccount");
        String remiPeopleAddress = req.getParameter("remiPeopleAddress");
        ProductCategory productCategory=new ProductCategory(prodId,prodChannelName,remiBankName,remiSwift,remiBankCode,remiCnaps,remiBankAddress,remiPeopleName,remiAccount,remiPeopleAddress);
        int rows = productCategoryService.addRemi(productCategory);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (rows > 0) {
            re.setReturnCode(CodeAndMsg.OPERATE_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_SUCCESS.getReturnMsg());
        } else {
            re.setReturnCode(CodeAndMsg.OPERATE_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //接收请求数据
        String prodIdStr = req.getParameter("prodId");
        Integer prodId = null;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.valueOf(prodIdStr);
        }
        //调用方法
        int rows = productCategoryService.deleteProd(prodId);
        ReturnEntity re = new ReturnEntity();
        if (rows > 0) {
            re.setReturnCode(CodeAndMsg.OPERATE_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_SUCCESS.getReturnMsg());
        } else {
            re.setReturnCode(CodeAndMsg.OPERATE_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

}
